﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using SATF.BI;

namespace SATF
{
    public partial class Default : System.Web.UI.Page
    {
        private BIUser m_user;
        private LogonInfo m_logonInfo;
        LogonInfosSingleton m_logonInfosSingleton;
        private static List<string> m_tabIDList;
        public List<string> TabIDList
        {
            get
            {
                if (m_tabIDList == null)
                {
                    m_tabIDList = new List<string>();
                }
                return m_tabIDList;
            }
            set { m_tabIDList = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //通过session验证是否登录
            if (Session["user"] == null || Session["user"].ToString() == "")
            {
                Response.Redirect("./Account/Logon.aspx");
                return;
            }
            string userName = Session["user"].ToString();
            m_logonInfosSingleton = LogonInfosSingleton.instance;

            if (!IsPostBack)
            {
                
                if (PageManager.Instance != null)
                {
                    HttpCookie themeCookie = Request.Cookies["Theme"];
                    if (themeCookie != null)
                    {
                        string themeValue = themeCookie.Value;
                        PageManager.Instance.Theme = (Theme)Enum.Parse(typeof(Theme), themeValue, true);
                    }

                    HttpCookie langCookie = Request.Cookies["Language"];
                    if (langCookie != null)
                    {
                        string langValue = langCookie.Value;
                        PageManager.Instance.Language = (Language)Enum.Parse(typeof(Language), langValue, true);
                    }
                }

                //获取登录用户信息
                this.lableNotice.Text = "欢迎您:" + userName;
                this.btnLogon.Text = "注销";
                m_user = new BIUser(userName);
                //如果是管理员，显示管理页面
                if (m_user.Group_ID == 1)
                {
                    this.AccordionPaneAdmin.Visible = true;
                }
       
                //绑定首页数据
                
            }
        }

        protected void ddlTheme_SelectedIndexChanged(object sender, EventArgs e)
        {
           // HttpCookie themeCookie = new HttpCookie("Theme", ddlTheme.SelectedValue);
            //themeCookie.Expires = DateTime.Now.AddYears(1);
           // Response.Cookies.Add(themeCookie);

           // PageContext.Refresh();
        }



        protected void TreeNodeCommand(object sender, ExtAspNet.TreeCommandEventArgs e)
        {
            if (!AssertIsLogon())
            {
                Alert.ShowInTop("您尚未登录!");
                return;
            }

            ExtAspNet.Tree tree = sender as ExtAspNet.Tree;
            string nodeID = tree.SelectedNodeID;
            ExtAspNet.TreeNode treeNode = tree.FindNode(nodeID);
            if (treeNode.Leaf)
            {
                string tabID = "dynamic_added_tab_" + treeNode.NodeID;
                PageContext.RegisterStartupScript(this.mainTabStrip.GetAddTabReference(
                    tabID,
                    treeNode.NavigateUrl, 
                    treeNode.Text, 
                    "./images/filetype/vs_aspx.png", 
                    true));
                //添加进自定义tabs，但tabs不真正管理TabStrip
                if (!this.TabIDList.Contains(tabID))
                {
                    this.TabIDList.Add(tabID);
                }
                
            }
           
        }

        protected void btnLogon_Clicked(object sender, EventArgs e)
        {
            if (AssertIsLogon())
            {
                //如果已经登录，则注销
                //清除cookie
                HttpCookie myCookie = new HttpCookie("satf_userinfo"); 
                myCookie.Expires = DateTime.Now.AddDays(-1d); 
                Response.Cookies.Add(myCookie);  

                this.lableNotice.Text = "您尚未登录";
                this.btnLogon.Text = "登录";
                
                HomePage.mytasks = null;
                this.DefaultTab.RefreshIFrame();
                //m_user = LogonInfosSingleton.instance[]
                LogonInfosSingleton.instance[Session["user"].ToString()].IsLogon = false;
                //清除所有打开的tab，从最后一个tab开始
                foreach (string tabID in this.TabIDList)
                {
                    PageContext.RegisterStartupScript(this.mainTabStrip.GetRemoveTabReference(tabID));
                }
                this.TabIDList = null;
                //清空session
                Session.RemoveAll();
            }
            else
            {
                Response.Redirect("./Account/Logon.aspx");
            }
    
        }

        protected void btnRegister_Clicked(object sender, EventArgs e)
        {
            Alert.ShowInTop("暂未实现");
        }

        protected void btnHelp_Clicked(object sender, EventArgs e)
        {
            Alert.ShowInTop("暂未实现");
            
        }

        protected void OnTreeNodeCheck(object sender, ExtAspNet.TreeCheckEventArgs e)
        {
            int i = 0;
        }

        private bool AssertIsLogon()
        {
            if (Session["user"] == null || Session["user"].ToString() == "")
            {
                return false;
            }
            string userName = Session["user"].ToString();

            LogonInfo info = LogonInfosSingleton.instance[userName];
            if (info == null || !info.IsLogon)
            {
                return false;
            }
            return true;
        }
    }
}